package com.mystudy.netty.http.https;

import com.mystudy.netty.echo.version_1.EchoServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

/**
 * @author 周志龙
 * @Email 15216876513@163.com
 * @Date on 2019/10/23 22:08
 * @Description 服务器引导类
 */
public class HttpServer {
    /**
     * logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(HttpServer.class);
    private int port = 0;

    public HttpServer(int port) {
        this.port = port;
    }

    public static void main(String[] args) throws InterruptedException {
        new HttpServer(10000).start();
    }

    public void start() throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        final EchoServerHandler serverHandler = new EchoServerHandler();
        bootstrap.group(group)
                .channel(NioServerSocketChannel.class)
                .localAddress(new InetSocketAddress(port))
                .childHandler(new HttpPipelineInitializer());
        try {
            ChannelFuture future = bootstrap.bind().sync();

            ChannelFuture closeFuture = future.channel().closeFuture();
            closeFuture.sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }

}
